package com.ylbh.modules.newinfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.PageDTO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.modules.newinfo.mapper.ShoppingNewsClassMapper;
import com.ylbh.modules.newinfo.entity.ShoppingNewsClass;
import com.ylbh.modules.newinfo.service.ShoppingNewsClassService;

import java.util.List;

@Service("shoppingNewsClassService")
public class ShoppingNewsClassServiceImpl extends ServiceImpl<ShoppingNewsClassMapper, ShoppingNewsClass> implements ShoppingNewsClassService {

    @Autowired
    private ShoppingNewsClassMapper shoppingNewsClassMapper;

    /**
     * 新闻分类列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryNewClassList(PageDTO pageDTO) {
        Page<Object> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<ShoppingNewsClass> list = shoppingNewsClassMapper.selectNewsClassList(pageDTO.getKeywords(), page);
        return new PageUtils(new Page<ShoppingNewsClass>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 新闻分类查看
     * @param id
     */
    @Override
    public ShoppingNewsClass getNewClassById(String id) {
        return shoppingNewsClassMapper.selectNewClassById(id);
    }

    /**
     * 新闻分类修改
     * @param shoppingNewsClass
     */
    @Override
    public void modifyNewClassById(ShoppingNewsClass shoppingNewsClass) {
        shoppingNewsClass.setImgPath("news_info");
        if (StringUtils.isNotBlank(shoppingNewsClass.getImgName())) {
            shoppingNewsClass.setImgName(shoppingNewsClass.getImgName().substring(shoppingNewsClass.getImgName().lastIndexOf("/") + 1));
        }
        shoppingNewsClassMapper.updateNewClass(shoppingNewsClass);
    }

    /**
     * 新闻分类新增
     * @param shoppingNewsClass
     */
    @Override
    public void addNewClass(ShoppingNewsClass shoppingNewsClass) {
        shoppingNewsClass.setCreateUserId(ShiroUtils.getUserId().intValue());
        shoppingNewsClass.setClassId(IdWorker.getNoId().toString());
        String key = shoppingNewsClassMapper.selectMaxClassKey();
        if (!StringUtils.isEmpty(key)) {
            shoppingNewsClass.setClassKey(Integer.parseInt(key) + 1);
            shoppingNewsClass.setImgPath("news_info");
            if (StringUtils.isNotBlank(shoppingNewsClass.getImgName())) {
                shoppingNewsClass.setImgName(shoppingNewsClass.getImgName().substring(shoppingNewsClass.getImgName().lastIndexOf("/") + 1));
            }
            shoppingNewsClassMapper.insertNewClass(shoppingNewsClass);
        }
    }

    /**
     * 删除新闻分类
     * @param list
     */
    @Override
    public void removeNewClassByIds(List<String> list) {
        shoppingNewsClassMapper.deleteNewClass(list);
    }
}
